//
//  RegViewController.m
//  MyShoes
//
//  Created by mac on 11-6-26.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ModiPwdViewController.h"
#import "iToast.h"
#import "AppDelegate.h"
#import "GlobalVar.h"


@interface ModiPwdViewController (PrivateMethods)
- (void)initTextField;
@end
@implementation ModiPwdViewController
@synthesize dataTableView;
@synthesize usernameField;
@synthesize passwordField;
@synthesize repasswordField;



-(void)viewDidLoad
{
    [super viewDidLoad];
	self.title = @"修改密码";
	[self initTextField];
	self.dataTableView = [[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped]autorelease];
	dataTableView.delegate = self;
	dataTableView.dataSource = self;
	//[dataTableView setBackgroundColor:[UIColor whiteColor]];
	//dataTableView.scrollEnabled = NO;
	[self.view addSubview:self.dataTableView];
	UIBarButtonItem *rightButton =[[[UIBarButtonItem alloc]initWithTitle:@"修改" 
																   style:UIBarButtonItemStyleDone 
																  target:self action:@selector(dosend:)]autorelease];	
	self.navigationItem.rightBarButtonItem = rightButton;
}

-(void)viewWillAppear:(BOOL)animated
{
	//[emailField becomeFirstResponder];
}

-(IBAction)dosend:(id)dosend
{
    NSString *nsStrPwd1 = self.passwordField.text;
    NSString *nsStrPwd2 = self.repasswordField.text;
    if ([self.usernameField.text isEqualToString:@""]||[nsStrPwd1 isEqualToString:@""]||[nsStrPwd2 isEqualToString:@""])
    {
        iToast* toast=[iToast makeText:@"请输入密码"];
        [toast show];
        return;
    }
    if (![nsStrPwd1 isEqualToString:nsStrPwd2])
    {
        iToast* toast=[iToast makeText:@"两次输入的新密码不一样"];
        [toast show];
         return;
    }
    [self textFieldShouldReturn:passwordField];
    [self textFieldShouldReturn:repasswordField];
    [self textFieldShouldReturn:usernameField];
	HUD = [[[MBProgressHUD alloc] initWithView:self.view]autorelease];
    HUD.removeFromSuperViewOnHide = YES;
	[self.view addSubview:HUD];
	[HUD setLabelFont:[UIFont systemFontOfSize:12]];
	[HUD setLabelText:@"正在提交,请稍候..."];
	[HUD show:YES];
    NSString* preMD5=[NSString stringWithFormat:@"%@%@%@",[GlobalVar loginUserKey],SOFT_ID,@"ysw"];
    NSString* md5Str=[GlobalVar md5Str:preMD5];
    ASIFormDataRequest* request;
    if ([GlobalVar sharedInstance].blnUseProxy==YES)
    {
        request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",origin_url_prefix,@"modipass.php"]]];
        [request setProxyPort:ProxyPort];
        [request setProxyHost:[GlobalVar getTheBestIp]];
    }else
    {
        request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",[GlobalVar getTheBestUrl],@"proxy.php"]]];
        [request setPostValue:origin_url_prefix forKey:@"apiurl"];
        [request setPostValue:@"modipass" forKey:@"act"];
    }
    [request setDelegate:self];
    [request setTimeOutSeconds:TIME_OUT_SECONDS];
    [request setPostValue:SOFT_ID forKey:@"softid"];
    [request setPostValue:[GlobalVar loginUserKey] forKey:@"mobile"];
    [request setPostValue:usernameField.text forKey:@"oldpass"];
    [request setPostValue:passwordField.text forKey:@"newpass"];
    [request setPostValue:md5Str forKey:@"md5"];
    [request setPostValue:MULTIAGENTSWITCH forKey:@"multiAgent"];
    [request setPostValue:NETWORKSTATE forKey:@"apn"];
    [request setPostValue:SOFT_VER forKey:@"version"];
    [request setDidFinishSelector:@selector(modiSuccess:)];
    [request setDidFailSelector:@selector(modiFail:)];
    [request setShouldAttemptPersistentConnection:NO];
    [request startAsynchronous];
}

-(void)modiSuccess:(ASIHTTPRequest*)request
{
    [HUD hide:YES];
    NSString* result=[request responseString];
    //NSLog(@"修改密码信息:%@",result);
    NSArray* resultArr=[result componentsSeparatedByString:@"|"];
    NSString* code=[resultArr objectAtIndex:0];
    if ([code isEqualToString:@"1"])
    {
        iToast* toast=[iToast makeText:@"密码修改成功"];
        [toast show];
        self.usernameField.text = self.passwordField.text = self.repasswordField.text= @"";
    }else
    {
        iToast* toast=[iToast makeText:[resultArr objectAtIndex:1]];
        [toast show];
    }
}
-(void)modiFail:(ASIHTTPRequest*)request
{
    iToast* toast=[iToast makeText:[NSString stringWithFormat:@"%@/%@",@"修改失败,",ERRORCODE]];
    [toast show];
}
-(void)LoginOut
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:@"LOGIN_USER_KEY"];
    [defaults removeObjectForKey:@"LOGIN_USER_NUMBER"];
    [defaults synchronize];
    [(AppDelegate*)[UIApplication sharedApplication].delegate logout];
}

-(void)poptoParent
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)returntoFrontView:(id)sender
{
	[self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView 
{ 
	return 1; 
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [NSString stringWithFormat:@"当前号码:%@",[GlobalVar loginUserKey]];
}

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section 
{
	return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *editableCellIdentifier = @"editableCellIdentifier";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
							 editableCellIdentifier];
	if (cell == nil)
    {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
									   reuseIdentifier:editableCellIdentifier] autorelease];
	}
	UIView *textField = [cell viewWithTag:100];
	if (textField)
    {
		[textField removeFromSuperview];
	}
	int row = [indexPath row];
	if (row == 0)
    {
		cell.textLabel.text = @"原密码：";
		[cell.contentView addSubview:usernameField];
	}else if (row == 1)
    {
		cell.textLabel.text = @"密码：";
		[cell.contentView addSubview:passwordField];
	}else if (row == 2)
    {
		cell.textLabel.text = @"确认密码：";
		[cell.contentView addSubview:repasswordField];
	}
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	return cell;
}

/**
 * 初始化TextField
 */
- (void)initTextField
{
	for (int i = 0; i < 3; i++)
    {
		UITextField *textField = [[[UITextField alloc] init] autorelease];
		textField.frame = CGRectMake(95, 10, 200.0, 25.0);
		textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        textField.keyboardType=UIKeyboardTypeNumberPad;
		textField.returnKeyType = UIReturnKeyDone;
        textField.secureTextEntry = YES;
		textField.delegate = self;
		textField.tag = 100;
		if (i == 0)
        {
			self.usernameField = textField;
			textField.placeholder = @"请输入原密码";
		}else if (i == 1)
        {
			self.passwordField = textField;
			textField.placeholder = @"请输入密码";
		}else if (i == 2)
        {
			self.repasswordField = textField;
			textField.placeholder = @"请输入确认密码";
		}
	}
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.isFirstResponder)
    {
        [textField resignFirstResponder];
    }
    /*
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3f];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    self.dataTableView.frame  = self.view.bounds;
    [UIView commitAnimations];
     */
	return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    /*
    if(self.dataTableView.frame.size.height !=200)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3f];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        self.dataTableView.frame  = CGRectMake(0, 0, 320, (iPhone5)?200+88:200);
        [UIView commitAnimations];
    }
    */
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{

}

- (void)dealloc {
   
	[repasswordField release];
	[usernameField release];
	[passwordField release];
	[dataTableView release];
    [super dealloc];
}


@end
